using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace LDNS.SDK.Commands.Models
{
    /// <summary>
    /// 获取服务统计命令类
    /// 作者: huobing
    /// </summary>
    public sealed class GetStatisticsCommand : BaseCommand<GetStatisticsResult>
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public const string NAME = "GET_STATISTICS";
        
        /// <summary>
        /// 获取命令名称
        /// </summary>
        public override string Name => NAME;
        
        /// <summary>
        /// 获取命令格式
        /// </summary>
        public override string Format => "GET_STATISTICS";
        
        /// <summary>
        /// 获取命令描述
        /// </summary>
        public override string Description => "获取服务统计";
        
        /// <summary>
        /// 获取命令权限级别
        /// </summary>
        public override CommandPermission Permission => CommandPermission.User;
        
        /// <summary>
        /// 验证命令参数
        /// </summary>
        /// <param name="args">命令参数数组</param>
        /// <param name="error">错误信息输出</param>
        /// <returns>验证是否通过</returns>
        public override bool Validate(string[] args, out string error) 
        {
            error = string.Empty;
            return args.Length == 0 || (error = "此命令不接受参数") == string.Empty;
        }
    }
    
    /// <summary>
    /// 服务统计命令的返回实体类
    /// 作者: huobing
    /// </summary>
    public class GetStatisticsResult : BaseCommandResult
    {

        /// <summary>
        /// DNS缓存记录总数
        /// </summary>
        public int DnsCacheCount { get; set; }
        
        /// <summary>
        /// 总查询次数
        /// </summary>
        public long TotalQueries { get; set; }
        
        /// <summary>
        /// 成功查询次数
        /// </summary>
        public long SuccessfulQueries { get; set; }
        
        /// <summary>
        /// 失败查询次数
        /// </summary>
        public long FailedQueries { get; set; }
        
        /// <summary>
        /// 平均响应时间（毫秒）
        /// </summary>
        public double AverageResponseTime { get; set; }
        
        /// <summary>
        /// 查询类型分布
        /// </summary>
        public Dictionary<string, long> QueryTypeDistribution { get; set; } = new Dictionary<string, long>();
        
        /// <summary>
        /// 内存使用情况（MB）
        /// </summary>
        public double MemoryUsageMB { get; set; }
        
        /// <summary>
        /// 控制台输出格式化
        /// </summary>
        /// <returns>格式化后的统计信息</returns>
        public override string ToConsoleString()
        {  
            var sb = new System.Text.StringBuilder();
            
            if (string.IsNullOrEmpty(Message))
            {
                sb.AppendLine("DNS服务统计信息：");
            }
            else
            {
                sb.AppendLine(Message);
            }
            
            // 由于IDnsService接口中没有这些属性，我们只返回缓存信息
            sb.AppendLine($"  缓存记录总数: {DnsCacheCount}");
            sb.AppendLine($"  总查询次数: {TotalQueries}");
            sb.AppendLine($"  成功查询: {SuccessfulQueries}");
            sb.AppendLine($"  失败查询: {FailedQueries}");
            sb.AppendLine($"  平均响应时间: {AverageResponseTime:F2} 毫秒");
            sb.AppendLine($"  内存使用: {MemoryUsageMB:F2} MB");
            
            if (QueryTypeDistribution.Count > 0)
            {
                sb.AppendLine("");
                sb.AppendLine("查询类型分布:");
                
                // 准备表格数据
                var tableData = new List<string[]>();
                tableData.Add(new[] { "查询类型", "次数" }); // 表头
                
                foreach (var kvp in QueryTypeDistribution)
                {
                    tableData.Add(new[] { kvp.Key, kvp.Value.ToString() });
                }
                
                // 使用TextTableFormatter格式化表格
                string table = LDNS.SDK.Formatting.TextTableFormatter.FormatAlignedTable(tableData, ' ', 60);
                sb.AppendLine(table);
            }
            
            return sb.ToString().TrimEnd();
        }
    }
}